package src.tree;

import src.Map;

/**
 * 基于二分搜索树的字典
 */
public class AVL<K extends Comparable<K>, V> implements Map<K, V>{

    private class Node{
        public K key;
        public V value;
        public Node left, right;
        public int height;
        public Node(K key, V value){
            this.key = key;
            this.value = value;
            left = null;
            right = null;
            height = 1;
        }
        @Override
        public String toString() {
            return key.toString() + ": " + value.toString();
        }
    }

    private Node root;
    private int size;

    public AVL(){
        root = null;
        size = 0;
    }

    private int getHeight(Node node){
        if (node == null) {
            return 0;
        }
        return node.height;
    }

    /**
     * 查询平衡因子
     * @param node
     * @return
     */
    private int getBalanceFactor(Node node){
        if (node == null) {
            return 0;
        }
        return Math.abs(getHeight(node.left) - getHeight(node.right));
    }

    @Override
    public void add(K key, V value) {
        root = add(root, key, value);
    }

    private Node add (Node node, K key, V value){
        if (node == null) {
            size ++;
            return new Node(key, value);
        }
        if (key.compareTo(node.key) < 0) {
            node.left = add(node.left, key, value);
        }else if(key.compareTo(node.key) > 0){
            node.right = add(node.right, key, value);
        }else{
            node.value = value;
        }
        // 维护节点的高度
        node.height = 1 + Math.max(getHeight(node.left), getHeight(node.right));
        // 计算平衡因子
        int balanceFactor = getBalanceFactor(node);
        if (balanceFactor > 1) {
            // 不满足平衡二叉树条件
            
        }
        return node;
    }

    
    private Node getNode(Node node, K key){
        if (node == null) {
            return null;
        }
        if (key.compareTo(node.key) == 0) {
            return node;
        }else if(key.compareTo(node.key) < 0){
            return getNode(node.left , key);
        }else{
            return getNode(node.right, key);
        }
    }


    @Override
    public V remove(K key) {
        Node node = getNode(root, key);
        if (node != null) {
            root = remove(root, key);
            return node.value;
        }
        return null;
    }

    private Node minNode(Node node){
        if (node == null) {
            return null;
        }
        Node cur = node;
        while(cur.left != null){
            cur = cur.left;
        }
        return cur;
    }

    private Node removeMin(Node node){
        if (node == null) {
            return null;
        }
        Node prev = node;
        while(prev.left != null){
            prev = prev.left;
        }
        prev.left = null;
        return node;
    }

    private Node remove(Node node, K key){
        if (node == null) {
            return null;
        }
        if (key.compareTo(node.key) < 0) {
            node.left = remove(node.left, key);
            return node;
        }else if(key.compareTo(node.key) > 0){
            node.right = remove(node.right, key);
        }else{
            if (node.left == null) {
                Node rigthTree = node.right;
                node.right = null;
                size --;
                return rigthTree;
            }else if(node.right == null){
                Node leftTree = node.left;
                node.left = null;
                size --;
                return leftTree;
            }else{
                Node succ = minNode(node.right);
                succ.right = removeMin(node.right);
                succ.left = node.left;
                node.left = node.right = null;
                return succ;
            }
        }
        return node;
    }
    @Override
    public boolean contains(K key) {
        return getNode(root, key) != null;
    }

    @Override
    public V get(K key) {
        Node node = getNode(root, key);
        return node == null ? null: node.value;
    }

    @Override
    public void set(K key, V newValue) {
        Node node = getNode(root, key);
        if (node == null) {
            throw new IllegalArgumentException(key + " is not exist");
        }
        node.value = newValue;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    private void printStr(Node node, StringBuffer res){
        if (node == null)
            return ;
        printStr(node.left, res);
        res.append(node + ", ");
        printStr(node.right, res);
    }

    @Override
    public String toString() {
        StringBuffer res = new StringBuffer();
        res.append("Map [");
        printStr(root, res);
        res.append(']');
        return res.toString();
    }
}